package com.ggg.clothesshop.controller;

import com.ggg.clothesshop.model.Address;
import com.ggg.clothesshop.service.AddressService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.Map;

@Controller
@RequestMapping("/address")
public class AddressController {

    @Autowired
    AddressService addressService;

    @ResponseBody
    @RequestMapping("/addAddress")
    public Map addAddress(Address address){
        Map map = new HashMap();
        map.put("status",500);
        map.put("data","对不起,添加地址信息失败！");
        if(addressService.addAddress(address)>0){
            map.put("status",200);
            map.put("data","添加地址成功");
        }
        return map;
    }

    @ResponseBody
    @RequestMapping("/deleteAddress")
    public Map deleteAddress(int addressid){
        Map map = new HashMap();
        map.put("status",500);
        map.put("data","对不起,删除地址信息失败！请刷新后重试");
        if(addressService.deleteAddress(addressid)>0){
            map.put("status",200);
            map.put("data","删除地址成功");
        }
        return map;
    }

    @ResponseBody
    @RequestMapping("/updateAddress")
    public Map updateAddress(Address address){
        System.out.println(address.toString());
        Map map = new HashMap();
        map.put("status",500);
        map.put("data","对不起,修改地址信息失败！");
        if(addressService.updateAddress(address)>0){
            map.put("status",200);
            map.put("data","修改地址成功");
        }
        return map;
    }

    //获取一个用户的一页地址的信息
    @ResponseBody
    @RequestMapping("/selectByPage")
    public Object selectByPage(
            @RequestParam(name = "pageNum", required = false, defaultValue = "1")
                    int pageNum,
            @RequestParam(name = "pageSize", required = false, defaultValue = "10")
                    int pageSize,int userid){
        return addressService.selectByPage(pageNum,pageSize,userid);
    }

    //获取一个用户的所有地址信息
    @ResponseBody
    @RequestMapping("/selectListAddress")
    public Object selectListAddress(int userid){

        return addressService.selectListAddress(userid);

    }

    //根据地址id获得地址信息
    @ResponseBody
    @RequestMapping("/selectById")
    public Address selectById(int addressid){

        return addressService.selectById(addressid);

    }

}
